package cn.com.recycleviewdemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.com.recycleviewdemo.R;

/**
 * 作者: 鹿文龙
 * 时间: 2017/11/16
 */

public class RecyclerGridViewAdapter extends RecyclerView.Adapter<RecyclerGridViewAdapter.ViewHolder> {

    private final int ON_CLICK_TAG_KEY = R.string.feedback_adapter_image_onClick;
    private final int ON_LONGCLICK_TAG_KEY = R.string.feedback_adapter_image_onLongClick;

    private final String ON_CLICK_TAG_VALUE_1 = "onThumbPictureClick";
    private final String ON_CLICK_TAG_VALUE_2 = "onAddPictureClick";


    private List<String> list;

    private Callback callback;

    private List<ImageView> imageViews = new ArrayList<>();

    public RecyclerGridViewAdapter(List<String> list, Callback callback) {
        this.list = list;
        this.callback = callback;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = parent.inflate(parent.getContext(), R.layout.listitem, null);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (null == holder) return;
        if (list != null && position < list.size()) {
            Glide.with(holder.imageView.getContext())
                    .load(list.get(position))
                    .error(R.mipmap.ic_launcher)
                    .placeholder(R.mipmap.ic_launcher)
                    .into(holder.imageView);
            imageViews.add(holder.imageView);
            holder.imageView.setTag(ON_CLICK_TAG_KEY, ON_CLICK_TAG_VALUE_1);
            holder.imageView.setTag(ON_LONGCLICK_TAG_KEY, position);
        } else {
            holder.imageView.setImageResource(R.drawable.ic_feed_back_add_img);
            holder.imageView.setTag(ON_CLICK_TAG_KEY, ON_CLICK_TAG_VALUE_2);
            holder.imageView.setTag(ON_LONGCLICK_TAG_KEY, -1);
        }


    }

    @Override
    public int getItemCount() {
        int count = list.size();
        count = count < 4 ? count + 1 : count;
        return count;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        private ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            imageView.setOnClickListener(this);
            imageView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (callback != null) {
                String onClickValue = (String) view.getTag(ON_CLICK_TAG_KEY);
                switch (onClickValue) {
                    case ON_CLICK_TAG_VALUE_1:
                        callback.onZoomInClick((ImageView) view, imageViews, list);
                        break;
                    case ON_CLICK_TAG_VALUE_2:
                        callback.onInserteClick();
                        break;
                }
            }
        }

        @Override
        public boolean onLongClick(View view) {
            int position = (Integer) view.getTag(ON_LONGCLICK_TAG_KEY);
            callback.onDeleteClick(position);
            return -1 != position;
        }
    }

    public interface Callback {

        void onInserteClick();

        void onDeleteClick(int i);

        void onZoomInClick(ImageView imageView, List<ImageView> imageGroupList, List<String> urlList);

    }
}
